<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-08 15:00
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Transformers;

use App\Lib\Utilities;
use League\Fractal\TransformerAbstract;

class GetPortfolioListTransformer extends TransformerAbstract
{
	private $list;
	private $isLike;
	private $isApp;
	private $memberId;

	public function __construct($list, $isLike = false, $isApp = false, $memberId = 0)
	{
		$this->list = $list;
		$this->isLike = $isLike;
		$this->isApp = $isApp;
		$this->memberId = $memberId;
	}

	public function transform()
	{
		$portfolioList = [];
		if ($this->isApp) {
			foreach ($this->list->items() as $v) {
//				$orderCounts = 0;
//				if(isset($v->orderItems)){
//					$orderCounts = $v->orderItems->toArray();
//					$orderCounts = array_column($orderCounts,'order');
//					$orderCounts = count(array_filter($orderCounts));
//				}

				$portfolioList[] = [
					"id"         => $v->id,
					"name"       => $v->name,
					"play_times" => $v->play_times,
					"created_at" => $v->created_at->format('Y-m-d H:i:s'),
					"desc"       => $v->desc,
					"order_count"=> $v->order_num,
					"video"      => empty($v->resource) ? null : $v->resource->video,
					"service"    => $v->storeService,
					"like_count" => $v->user_portfolio_like_count,
				];
			}
		} else {
			foreach ($this->list->items() as $v) {
				$this->memberId = empty($this->memberId) ? $v->member_id : $this->memberId;
				$v = $this->isLike ? $v->portfolio : $v;
				$likeNum = $this->isLike ? (isset($v->userPortfolioLike) ? count(($v->userPortfolioLike->toArray())) : 0) : $v->user_portfolio_like_count;
				$userLikeList = isset($v->user->userLike) ? $v->user->userLike->toArray() : [];
				$userLike = empty($userLikeList) ? [] : array_column($userLikeList, 'member_id');
				$isLikeData = isset($v->userPortfolioLike) ? $v->userPortfolioLike : [];
				$isLikeData = is_object($isLikeData) ? $isLikeData->toArray() : [];
				$isLikeData = empty($isLikeData) ? [] : array_column($isLikeData, 'portfolio_id');

				if (isset($v)) {
					$portfolioList[] = [
						"id"              => $v->id,
						"barberId"        => $v->user_id,
						"portfolioName"   => $v->name,
						"portfolio_desc"  => $v->desc,
						"serviceId"       => $v->service_id,
						"playTimes"       => $v->play_times,
						"date"            => $v->created_at->format('Y-m-d H:i:s'),
						"barberName"      => empty($v->user) ? null : $v->user->nick,
						"videoUrl"        => empty($v->resource) ? null : empty($v->resource->video) ? null : $v->resource->video->url,
						"videoThumbUrl"   => empty($v->resource) ? null : empty($v->resource->video) ? null : $v->resource->video->thumb,
						"userPortraitUrl" => empty($v->user) ? null : empty($v->user->picture) ? null : $v->user->picture->image,
						"store_id"        => $v->store_id,
						"serviceName"     => empty($v->storeService) ? null : $v->storeService->name,
						"servicePrice"    => empty($v->storeService) ? null : $v->storeService->price,
						"storeName"       => empty($v->store) ? null : $v->store->name,
						"address"         => empty($v->store) ? null : $v->store->address,
						"doorplate"       => empty($v->store) ? null : $v->store->doolplate,
						"level"      => empty($v->store) ? null : Utilities::getStoreLevel($v->store->order_sum),
						"likeNum"         => $likeNum,
						"isLike"          => in_array($v->id, $isLikeData) ? 1 : 0,
						"isMemberLike"    => in_array($this->memberId, $userLike) ? 1 : 0,
						"isDeleted"  => isset($v->deleted_at) ? 1 : 0
					];
				}
			}
		}
		$result = $this->list->toArray();
		$result['data'] = $portfolioList;
		return $result;
	}
}
